<?php

/**
 * Landmark form base class.
 *
 * @method Landmark getObject() Returns the current form's model object
 *
 * @package    lbreminder
 * @subpackage form
 * @author     Your name here
 */
abstract class BaseLandmarkForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                 => new sfWidgetFormInputHidden(),
      'name'               => new sfWidgetFormInputText(),
      'latitude'           => new sfWidgetFormInputText(),
      'longitude'          => new sfWidgetFormInputText(),
      'address'            => new sfWidgetFormInputText(),
      'street'             => new sfWidgetFormInputText(),
      'suburb'             => new sfWidgetFormInputText(),
      'state'              => new sfWidgetFormInputText(),
      'postcode'           => new sfWidgetFormInputText(),
      'description'        => new sfWidgetFormInputText(),
      'category_id'        => new sfWidgetFormPropelChoice(array('model' => 'LandmarkCategory', 'add_empty' => false)),
      'user_landmark_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
    ));

    $this->setValidators(array(
      'id'                 => new sfValidatorPropelChoice(array('model' => 'Landmark', 'column' => 'id', 'required' => false)),
      'name'               => new sfValidatorString(array('max_length' => 100)),
      'latitude'           => new sfValidatorNumber(),
      'longitude'          => new sfValidatorNumber(),
      'address'            => new sfValidatorString(array('max_length' => 200, 'required' => false)),
      'street'             => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'suburb'             => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'state'              => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'postcode'           => new sfValidatorString(array('max_length' => 10, 'required' => false)),
      'description'        => new sfValidatorString(array('max_length' => 500, 'required' => false)),
      'category_id'        => new sfValidatorPropelChoice(array('model' => 'LandmarkCategory', 'column' => 'id')),
      'user_landmark_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('landmark[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Landmark';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['user_landmark_list']))
    {
      $values = array();
      foreach ($this->object->getUserLandmarks() as $obj)
      {
        $values[] = $obj->getUserId();
      }

      $this->setDefault('user_landmark_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveUserLandmarkList($con);
  }

  public function saveUserLandmarkList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['user_landmark_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(UserLandmarkPeer::LANDMARK_ID, $this->object->getPrimaryKey());
    UserLandmarkPeer::doDelete($c, $con);

    $values = $this->getValue('user_landmark_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new UserLandmark();
        $obj->setLandmarkId($this->object->getPrimaryKey());
        $obj->setUserId($value);
        $obj->save();
      }
    }
  }

}
